//
//  ColumnChartView.m
//  MoneyLog
//
//  Created by wicresoft on 6/14/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "ColumnChartView.h"
#import "ColumnChartItem.h"

static int MARGIN_LEFT = 50;
static int MARGIN_BOTTOM = 50;
static int MARGIN_TOP = 10;
static int SHOW_SCALE_NUM = 10;

@interface ColumnChartView(private)
-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect;
-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect;
-(void)calcScales:(CGRect)_rect;
@end

@implementation ColumnChartView
@synthesize groupData;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        columnWidth=30;
        sideWidth = 6;
        self.backgroundColor=[UIColor whiteColor];
    }
    return self;
}

- (void) dealloc
{
	[groupData release];
	[super dealloc];
}


-(void)drawRect:(CGRect)_rect{
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
	CGContextFillRect(context, _rect);
	
	//计算刻度
	[self calcScales:_rect];
	
	//画刻度
	[self drawScale:context rect:_rect];
	
	//画柱
	[self drawColumn:context rect:_rect];
    
}

-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect{
	int width = [self getWidth];
    
    CGPoint points[3];
	points[0] = CGPointMake(MARGIN_LEFT - 10, MARGIN_TOP);
	points[1] = CGPointMake(MARGIN_LEFT - 10, _rect.size.height - MARGIN_BOTTOM + 1);
	points[2] = CGPointMake(width, _rect.size.height - MARGIN_BOTTOM + 1);
	CGContextSetAllowsAntialiasing(context, TRUE);
	CGContextAddLines(context, points, 3);
	
	CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
	
	for(int i=1;i<SHOW_SCALE_NUM + 1; i++){
        
		maxScaleHeight = (_rect.size.height - MARGIN_BOTTOM) * ( i ) / (SHOW_SCALE_NUM + 1);
		int vScal = ceil(1.0 * maxScaleValue / (SHOW_SCALE_NUM ) * (i ));
		float y = (_rect.size.height - MARGIN_BOTTOM) - maxScaleHeight;
		
		NSString *scaleStr = [NSString stringWithFormat:@"%d",vScal];
		[scaleStr
         drawAtPoint:CGPointMake(MARGIN_LEFT - 20 - [scaleStr sizeWithFont:[UIFont systemFontOfSize:12]].width, y - 10) withFont:[UIFont systemFontOfSize:12]];
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(MARGIN_LEFT - 13, y);
		CGContextSetLineDash(context, 0, NULL, 0);
		CGContextAddLines(context, points, 2);
		CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        
		CGContextDrawPath(context, kCGPathStroke);
		
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(width , y);
		float partren[] = {2,3};
		CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1].CGColor);
        
		CGContextSetLineDash(context, 0,partren , 2);
		CGContextAddLines(context, points, 2);
		CGContextDrawPath(context, kCGPathStroke);
		
	}
	
	CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    
	CGContextDrawPath(context, kCGPathStroke);
	CGContextSetAllowsAntialiasing(context, YES);
    
	
}

-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect{
	int vNumber = 0;
	int baseGroundY = _rect.size.height - MARGIN_BOTTOM, baseGroundX = MARGIN_LEFT;
	CGPoint points[4];
	
	UIColor *columnColor = [UIColor redColor];
	
	CGContextSetFillColorWithColor(context, columnColor.CGColor);
	CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
	
    vNumber = 0;
	for(ColumnChartItem *item in groupData){
        for (int i=0; i<2; i++) { 
            columnColor = (i==0)? [UIColor redColor] :[UIColor blueColor];
			NSNumber *value = (i==0)? [NSNumber numberWithFloat:item.firstValue] : [NSNumber numberWithFloat:item.secondValue];
            int offset = (i==0)? vNumber * 10 :(vNumber-1) * 10;
            NSLog(@"offset=%d",offset);
            float columnHeight = [value floatValue] / maxScaleValue * maxScaleHeight ;
            
            //标题
            if ([value intValue] !=0) {
                CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
                NSString *percent=[NSString stringWithFormat:@"%d",[value intValue]];
                [percent drawAtPoint:CGPointMake(offset + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY - columnHeight-20) withFont:[UIFont systemFontOfSize:8]];
            }
            
            //柱名
            if (i==0) {
                CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
                [item.title drawAtPoint:CGPointMake(offset + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY + 10) withFont:[UIFont systemFontOfSize:12]];
            }
			
			//画正面
			CGContextSetFillColorWithColor(context, columnColor.CGColor);
           
			CGContextAddRect(context, CGRectMake( baseGroundX + columnWidth * vNumber + offset
												 , baseGroundY - columnHeight 
												 , columnWidth
												 , columnHeight));
			CGContextDrawPath(context, kCGPathFill);
			//NSLog(@"columnHeight:%f, (_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ):%f",columnHeight,(_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ));
            
			if(columnHeight < 10){
				vNumber++;
				continue;
			}
			
			//画右侧面
            if (i%2==0) {
                CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.9 green:0 blue:0 alpha:1].CGColor);
            }
            else 
            {
                CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:0 blue:.9 alpha:1].CGColor);
            }
			
			points[0] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight -10);
			points[1] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY );
			points[3] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
			
			//画上面
            if (i%2==0) {
                CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:.4 blue:.4 alpha:1].CGColor);
            }
            else {
                CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.4 green:.4 blue:1 alpha:1].CGColor);
            }
			
			points[0] = CGPointMake(offset + baseGroundX + columnWidth * vNumber , baseGroundY - columnHeight );
			points[1] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth , baseGroundY - columnHeight -10 );
			points[3] = CGPointMake(offset + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
            
            			
			vNumber++;
		}
	}
}

-(void)calcScales:(CGRect)_rect{
	int columnCount = 0;
	for(ColumnChartItem *item in groupData){
        for (int i=0; i<2; i++) { 
            NSNumber *value = (i==0)? [NSNumber numberWithFloat:item.firstValue] : [NSNumber numberWithFloat:item.secondValue];
            
			if(maxValue<[value floatValue]) maxValue = [value floatValue];
			if(minValue>[value floatValue]) minValue = [value floatValue];
		}
        columnCount++;
	}
	
	maxScaleValue = ((int)ceil(maxValue) + (SHOW_SCALE_NUM - (int)ceil(maxValue) % SHOW_SCALE_NUM));
    //columnWidth=30;
    //sideWidth = 6;
}

-(int) getWidth {
    CGRect appRect=[[UIScreen mainScreen] applicationFrame];
    int width = MARGIN_LEFT+([self.groupData count]+1)*(2*columnWidth + sideWidth + 10);
    
    if (width<appRect.size.width) {
        width = appRect.size.width;
    }
    
    return width;
}

@end
